/***************************************************************************
 创建者：       hualei
 开始时间：    2018.09.10
 copyright:(C) 深圳市华友高科有限公司
 修改说明：（每次有修改就添加一条，带有 修改人，修改时间，修改描述）

 ***************************************************************************/
#include "tcpwriteinstructiondelegate.h"
#include <QDebug>

TCPWriteInstructionDelegate::TCPWriteInstructionDelegate(QWidget *parent)
    : QWidget(parent)
{
}

void TCPWriteInstructionDelegate::initialView()
{
    tcpWriteInstructionView->tcpWriteConstValue = "1";
    tcpWriteInstructionView->tcpWritePort = 1;
    tcpWriteInstructionView->tcpWriteClientIPVec.resize(4);
    tcpWriteInstructionView->tcpWriteServerIPVec.resize(4);
    tcpWriteInstructionView->tcpWriteBusType = E_TCP_WRITE_BUS_CLIENT;
    tcpWriteInstructionView->tcpWriteRegBegAddress = 1;
    tcpWriteInstructionView->tcpWriteType = E_TCP_WRITE_TYPE_REGISTER;
    tcpWriteInstructionView->regLengthValue = 1;
    tcpWriteInstructionView->jmpLBValue = 1;
    tcpWriteInstructionView->noteStr = "No NoteStr!";
}

void TCPWriteInstructionDelegate::cleanNoteSlot()
{
    tcpWriteInstructionView->noteStr.clear();
}

void TCPWriteInstructionDelegate::finishEditInstructionSlot()
{
    tcpWriteInstructionView->instructionOut = "TCP_WRITE ";

    if(tcpWriteInstructionView->tcpWriteType == E_TCP_WRITE_TYPE_CONST)
    {
        tcpWriteInstructionView->instructionOut += tcpWriteInstructionView->tcpWriteConstValue + ",";
    }
    else if(tcpWriteInstructionView->tcpWriteType == E_TCP_WRITE_TYPE_REGISTER)
    {
        tcpWriteInstructionView->instructionOut += tr("R[") + QString::number(tcpWriteInstructionView->tcpWriteRegBegAddress) + "],";
        tcpWriteInstructionView->instructionOut += tr("R[") + QString::number(tcpWriteInstructionView->regLengthValue) + "],";
    }

    if(tcpWriteInstructionView->tcpWriteBusType == E_TCP_WRITE_BUS_CLIENT)
    {
        tcpWriteInstructionView->instructionOut += QString::number(tcpWriteInstructionView->tcpWriteServerIPVec[0]) + ".";
        tcpWriteInstructionView->instructionOut += QString::number(tcpWriteInstructionView->tcpWriteServerIPVec[1]) + ".";
        tcpWriteInstructionView->instructionOut += QString::number(tcpWriteInstructionView->tcpWriteServerIPVec[2]) + ".";
        tcpWriteInstructionView->instructionOut += QString::number(tcpWriteInstructionView->tcpWriteServerIPVec[3]) + ",";
        tcpWriteInstructionView->instructionOut += QString::number(tcpWriteInstructionView->tcpWritePort) + ",";
        tcpWriteInstructionView->instructionOut += tr("CLIENT") + ",";
    }
    else if(tcpWriteInstructionView->tcpWriteBusType == E_TCP_WRITE_BUS_SERVER)
    {
        tcpWriteInstructionView->instructionOut += QString::number(tcpWriteInstructionView->tcpWriteClientIPVec[0]) + ".";
        tcpWriteInstructionView->instructionOut += QString::number(tcpWriteInstructionView->tcpWriteClientIPVec[1]) + ".";
        tcpWriteInstructionView->instructionOut += QString::number(tcpWriteInstructionView->tcpWriteClientIPVec[2]) + ".";
        tcpWriteInstructionView->instructionOut += QString::number(tcpWriteInstructionView->tcpWriteClientIPVec[3]) + ",";
        tcpWriteInstructionView->instructionOut += QString::number(tcpWriteInstructionView->tcpWritePort) + ",";
        tcpWriteInstructionView->instructionOut += tr("SERVER") + ",";
    }

    tcpWriteInstructionView->instructionOut += tr("JMP ") + tr("LB[") + QString::number(tcpWriteInstructionView->jmpLBValue) + "] ";
    tcpWriteInstructionView->instructionOut += tr(" //") + tcpWriteInstructionView->noteStr;

    qDebug() << "TCPWriteInstruction InstructionOut = " << tcpWriteInstructionView->instructionOut;

}

